package cn.edu.xmist.bullet;

import cn.edu.xmist.Direction;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Cannonball implements AbstractBullet {

    private BufferedImage image;

    private final Direction direction;

    private final int speed = 5;

    private int x;

    private int y;

    private Frame frame;

    public Cannonball(Frame frame, Direction direction, int x, int y) {
        this.direction = direction;
        this.x = x;
        this.y = y;
        this.frame = frame;
        try {
            this.image = ImageIO.read(Cannonball.class.getClassLoader().getResourceAsStream("./img/tankmissile.gif"));
        } catch (IOException e) {
            System.out.println("子弹图片加载异常");
            e.printStackTrace();
        }
    }

    @Override
    public Image getImage() {
        return this.image;
    }

    @Override
    public int getX() {
        switch (this.direction) {
            case LEFT:
                //坦克方向： <-
                //子弹的位置：x = 位置 - （坦克图片的高度 - 子弹的高度）/2
                this.x -= (60 - 17)/2;
                this.x -= this.speed;
                break;
            case RIGHT:
                //坦克方向： <-
                //子弹的位置：x = 位置 + （坦克图片的高度 - 子弹的高度）/2
                this.x += (60 - 17)/2;
                this.x += this.speed;
                break;
        }
        return this.x + 18;
    }

    @Override
    public int getY() {
        switch (this.direction) {
            case UP:
                //坦克方向： <-
                //子弹的位置：x = 位置 - （坦克图片的高度 - 子弹的高度）/2
                this.y -= (60 - 17)/2;
                this.y -= this.speed;
                break;
            case DOWN:
                //坦克方向： <-
                //子弹的位置：x = 位置 + （坦克图片的高度 - 子弹的高度）/2
                this.y += (60 - 17)/2;
                this.y += this.speed;
                break;
        }
        return this.y + 15;
    }

    @Override
    public boolean cancleable() {
        return this.x < 0 || this.x > this.frame.getWidth() || this.y < 0 || this.y > this.frame.getHeight();
    }
}
